import {
  request
} from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';
// pages/search/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    goods: []
  },
  TimeId: -1,
  inputValue(e) {
    const {
      value
    } = e.detail
    if (!value.trim()) {
      this.setData({
        value: '',
        goods: []
      })
      //没有输入
      return;
    }
    this.setData({
      value
    })
    //先清除之前开启的定时器
    clearTimeout(this.TimeId)
    this.TimeId = setTimeout(() => {
      this.getMessage(value)
    }, 888)
  },
  async getMessage(query) {
    const res = await request({
      url: `/goods/qsearch?query=${query}`
    })
    console.log(res)
    this.setData({
      goods: res
    })
  },
  cancel() {
    this.setData({
      value: '',
      goods: []
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //清空数据
    this.setData({
      value: '',
      goods: []
    })
    wx.showLoading({
      title: '加载中',
      mask: true,
      direction: 1200
    });
    setTimeout(() => {
      //停止下拉刷新
      wx.stopPullDownRefresh()
      wx.hideLoading(); //隐藏加载动画
    }, 1200)
  }
})